package com.example.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hutool.json.JSONUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Aspect
@Component

public class WebLogFilter {
//    private static final Logger LOGGER = LoggerFactory.getLogger(WebLogFilter.class);
//    private static ThreadLocal<Long> startTime = new ThreadLocal<Long>();
//    private static ThreadLocal<String> key = new ThreadLocal<String>();
//    private static  ObjectMapper objectMapper = new ObjectMapper();
//    
//    @Pointcut("execution(public * com.example.controller.*.*(..))")
//    public void webLog() {
//    }
//
//    @Before("webLog()")
//    public void doBefore(JoinPoint joinPoint) throws Throwable {
//
//    }
//
//    @Around("webLog()")
//    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
// 	     // 请求开始时间
//	     startTime.set(System.currentTimeMillis());
//	     System.out.println("时间：" + startTime);
//	     RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//	     ServletRequestAttributes sra = (ServletRequestAttributes) ra;
//	     HttpServletRequest request = sra.getRequest();
//	     
//	     // 如果有session则返回session如果没有则返回null(避免创建过多的session浪费内存)
//	     HttpSession session = request.getSession(false);
//	     // 获取请求头
//	     Enumeration<String> enumeration = request.getHeaderNames();
//	     HashMap<String, String> headers = new HashMap<String, String>();
//	     while (enumeration.hasMoreElements()) {
//	         String name = enumeration.nextElement();
//	         String value = request.getHeader(name);
//	         headers.put(name, value);
//	     }
//	     headers.forEach((key, value) -> {
//		   // 日志中输出所有请求头
//		   System.out.println(String.format("'%s' = %s", key, value));
//		});
//	     Object result = joinPoint.proceed();
//
//	     return result;
//    }


}